<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品滑块组件</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f8f9fa, #e9ecef);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            width: 100%;
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 50px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            padding: 30px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            font-size: 2.5rem;
            color: #333;
            margin-bottom: 10px;
            position: relative;
            display: inline-block;
        }

        .header h1:after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 80px;
            height: 4px;
            background: linear-gradient(to right, #42b883, #347474);
            border-radius: 2px;
        }

        .header p {
            color: #777;
            font-size: 1.1rem;
            max-width: 600px;
            margin: 0 auto;
            line-height: 1.6;
        }

        .css-slider-viewport {
            width: 100%;
            position: relative;
            margin-bottom: 40px;
        }

        .slider-controls {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 20px;
            margin-top: 25px;
        }

        .nav-btn {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background: white;
            border: 2px solid #42b883;
            color: #42b883;
            font-size: 1.2rem;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .nav-btn:hover {
            background: #42b883;
            color: white;
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(66, 184, 131, 0.3);
        }

        .nav-btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .pagination {
            display: flex;
            gap: 12px;
            justify-content: center;
        }

        .page-indicator {
            width: 14px;
            height: 14px;
            border-radius: 50%;
            background: #e0e0e0;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .page-indicator.active {
            background: #42b883;
            transform: scale(1.2);
        }

        .css-slider-holder {
            overflow: hidden;
            scroll-behavior: smooth;
            cursor: grab;
            user-select: none;
            border-radius: 16px;
            background: white;
            padding: 15px 5px;
            box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.05);
            position: relative;
        }

        .css-slider-holder:before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 20px;
            background: linear-gradient(to bottom, rgba(255,255,255,0.8), rgba(255,255,255,0));
            z-index: 2;
            border-radius: 16px 16px 0 0;
        }

        .css-slider-holder:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            height: 20px;
            background: linear-gradient(to top, rgba(255,255,255,0.8), rgba(255,255,255,0));
            z-index: 2;
            border-radius: 0 0 16px 16px;
        }

        .grid {
            display: grid;
            grid-gap: 26px;
            user-select: none;
        }

        .grid--slider {
            grid-auto-flow: column;
            grid-template-columns: calc(var(--col-size));
            grid-auto-columns: calc(var(--col-size));
            position: relative;
            padding: 10px 0;
        }

        .product-card {
            display: flex;
            flex-direction: column;
            overflow: visible;
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            height: 100%;
            position: relative;
            overflow: hidden;
        }

        .product-card:hover {
            transform: translateY(-10px);
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
        }

        .product-image {
            height: 200px;
            width: 100%;
            background: linear-gradient(45deg, #42b883, #347474);
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 3rem;
            position: relative;
            overflow: hidden;
        }

        .product-image i {
            opacity: 0.8;
        }

        .product-info {
            padding: 20px;
            flex-grow: 1;
            display: flex;
            flex-direction: column;
        }

        .product-title {
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }

        .product-description {
            color: #777;
            font-size: 0.95rem;
            line-height: 1.5;
            margin-bottom: 15px;
            flex-grow: 1;
        }

        .product-price {
            font-size: 1.5rem;
            font-weight: 700;
            color: #42b883;
            margin-top: 5px;
        }

        .product-meta {
            display: flex;
            justify-content: space-between;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 1px solid #f0f0f0;
            color: #999;
            font-size: 0.9rem;
        }

        .product-rating {
            color: #ffc107;
        }

        .status-indicator {
            position: absolute;
            top: 15px;
            right: 15px;
            background: #ff6b6b;
            color: white;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
            box-shadow: 0 4px 10px rgba(255, 107, 107, 0.3);
        }

        .status-new {
            background: #42b883;
        }

        .status-sale {
            background: #ff9f43;
        }

        .drag-indicator {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            color: #aaa;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 8px;
            z-index: 3;
        }

        .drag-indicator i {
            animation: pulse 1.5s infinite;
        }

        @keyframes pulse {
            0% { transform: translateX(0); }
            50% { transform: translateX(5px); }
            100% { transform: translateX(0); }
        }

        @media (max-width: 1023px) {
            .container {
                padding: 20px;
            }

            .header h1 {
                font-size: 2rem;
            }
        }

        @media (max-width: 767px) {
            .header h1 {
                font-size: 1.8rem;
            }

            .nav-btn {
                width: 42px;
                height: 42px;
                font-size: 1rem;
            }
        }
    </style>
</head>
<body>
<div id="app">
    <div class="container">
        <div class="header">
            <h1>热门商品推荐</h1>
            <p>发现我们精心挑选的优质商品，满足您的各种需求</p>
        </div>

        <div class="css-slider-viewport">
            <div ref="slider" class="css-slider-holder"
                 @mousedown="handleMouseDown"
                 @mouseleave="handleMouseUp"
                 @mousemove="handleMouseMove"
                 @mouseup="handleMouseUp">
                <div
                        :style="{
              '--visible-cols': visibleCols,
              '--col-gap': '26px',
              '--col-size': `calc((100% - var(--col-gap) * (var(--visible-cols) - 1)) / var(--visible-cols))`
            }"
                        class="grid grid--slider slider"
                >
                    <div
                            v-for="(goods, index) in itemsComputed"
                            :id="`grid_slider_active_${index}`"
                            :key="index"
                            class="product-card"
                    >
                        <div class="product-image">
                            <i :class="goods.icon"></i>
                            <div class="status-indicator" :class="'status-' + goods.status">
                                {{ goods.status === 'new' ? '新品' : goods.status === 'sale' ? '特价' : '热卖' }}
                            </div>
                        </div>
                        <div class="product-info">
                            <h3 class="product-title">{{ goods.title }}</h3>
                            <p class="product-description">{{ goods.description }}</p>
                            <div class="product-price">¥{{ goods.price.toFixed(2) }}</div>
                            <div class="product-meta">
                                <div class="product-rating">
                                    <i class="fas fa-star"></i>
                                    <i class="fas fa-star"></i>
                                    <i class="fas fa-star"></i>
                                    <i class="fas fa-star"></i>
                                    <i class="fas fa-star-half-alt"></i>
                                    ({{ goods.rating }})
                                </div>
                                <div class="product-sales">
                                    <i class="fas fa-shopping-cart"></i> {{ goods.sales }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="drag-indicator">
                    <i class="fas fa-arrows-alt-h"></i> 左右拖拽查看更多
                </div>
            </div>
        </div>

        <div class="slider-controls">
            <button class="nav-btn" @click="slide_left" :disabled="active === 1">
                <i class="fas fa-chevron-left"></i>
            </button>

            <div class="pagination">
                <div v-for="page in totalPages"
                     :key="page"
                     class="page-indicator"
                     :class="{ active: page === active }"
                     @click="slide_position(page)"></div>
            </div>

            <button class="nav-btn" @click="slide_right" :disabled="active === totalPages">
                <i class="fas fa-chevron-right"></i>
            </button>
        </div>
    </div>
</div>

<script>
    const { createApp, ref, computed, onMounted, onUnmounted } = Vue

    createApp({
        setup() {
            const slider = ref(null)
            const active = ref(1)
            const totalPages = ref(0)
            const items = ref([])

            const getVisibleCols = () => {
                if (window.innerWidth < 768) return 2
                if (window.innerWidth < 1024) return 3
                return 4
            }

            const visibleCols = ref(getVisibleCols())

            // 模拟商品数据
            const mockGoods = () => {
                const goods = []
                const icons = ['fas fa-laptop', 'fas fa-mobile-alt', 'fas fa-headphones', 'fas fa-camera', 'fas fa-tablet-alt', 'fas fa-tv', 'fas fa-gamepad', 'fas fa-clock']
                const statuses = ['new', 'sale', 'hot']
                const titles = [
                    "高性能笔记本电脑",
                    "旗舰智能手机",
                    "降噪蓝牙耳机",
                    "专业单反相机",
                    "轻薄平板电脑",
                    "4K超清电视",
                    "游戏主机套装",
                    "智能手表"
                ]

                const descriptions = [
                    "强大的处理器和大内存，满足工作和娱乐需求",
                    "超清全面屏，旗舰级摄像系统，全天续航",
                    "主动降噪技术，沉浸式音乐体验",
                    "专业级影像传感器，捕捉每一个精彩瞬间",
                    "轻薄便携，性能强劲，办公娱乐两不误",
                    "极致画质，震撼音效，家庭影院体验",
                    "高性能游戏主机，畅玩3A大作",
                    "健康监测，运动追踪，智能生活助手"
                ]

                for (let i = 0; i < 12; i++) {
                    const iconIndex = i % icons.length
                    const statusIndex = i % statuses.length
                    const titleIndex = i % titles.length

                    goods.push({
                        id: i + 1,
                        icon: icons[iconIndex],
                        status: statuses[statusIndex],
                        title: titles[titleIndex],
                        description: descriptions[titleIndex],
                        price: Math.floor(Math.random() * 2000) + 999,
                        rating: (Math.random() * 2 + 3).toFixed(1),
                        sales: Math.floor(Math.random() * 500) + 100
                    })
                }

                return goods
            }

            items.value = mockGoods()

            const itemsComputed = computed(() => {
                return items.value
            })

            // 计算总页数
            const calculateTotalPages = () => {
                if (!itemsComputed.value.length) return 0
                return Math.ceil(itemsComputed.value.length / visibleCols.value)
            }

            // 更新总页数
            const updateTotalPages = () => {
                totalPages.value = calculateTotalPages()
                if (active.value > totalPages.value) {
                    active.value = totalPages.value
                }
            }

            // 滚动到指定位置
            const scrollToPosition = (position) => {
                if (!slider.value) return

                const container = slider.value
                const scrollWidth = container.scrollWidth
                const pageWidth = container.clientWidth
                const maxScroll = scrollWidth - pageWidth

                // 计算目标滚动位置
                let targetScroll = (position - 1) * pageWidth
                targetScroll = Math.min(targetScroll, maxScroll)

                container.scrollTo({
                    left: targetScroll,
                    behavior: 'smooth'
                })
            }

            const slide_position = (position) => {
                if (position < 1 || position > totalPages.value) return
                active.value = position
                scrollToPosition(position)
            }

            const slide_right = () => {
                if (active.value < totalPages.value) {
                    active.value += 1
                    scrollToPosition(active.value)
                }
            }

            const slide_left = () => {
                if (active.value > 1) {
                    active.value -= 1
                    scrollToPosition(active.value)
                }
            }

            const handleResize = () => {
                const newCols = getVisibleCols()
                if (newCols !== visibleCols.value) {
                    visibleCols.value = newCols
                    updateTotalPages()
                    // 重新计算当前页
                    if (slider.value) {
                        const container = slider.value
                        const pageWidth = container.clientWidth
                        const currentPage = Math.round(container.scrollLeft / pageWidth) + 1
                        active.value = Math.min(currentPage, totalPages.value)
                    }
                }
            }

            onMounted(() => {
                window.addEventListener('resize', handleResize)
                updateTotalPages()
            })

            onUnmounted(() => {
                window.removeEventListener('resize', handleResize)
            })

            // 拖拽功能
            const isDragging = ref(false)
            const startX = ref(0)
            const startScrollLeft = ref(0)
            let animationId = ref(null)

            const handleMouseDown = (e) => {
                if (!slider.value) return
                isDragging.value = true
                startX.value = e.pageX - slider.value.offsetLeft
                startScrollLeft.value = slider.value.scrollLeft
                slider.value.style.cursor = 'grabbing'
            }

            const handleMouseMove = (e) => {
                if (!isDragging.value || !slider.value) return
                e.preventDefault()

                if (animationId.value) {
                    cancelAnimationFrame(animationId.value)
                }

                animationId.value = requestAnimationFrame(() => {
                    const x = e.pageX - slider.value.offsetLeft
                    const walk = (x - startX.value) * 1.5 // 控制滑动速度
                    slider.value.scrollLeft = startScrollLeft.value - walk
                })
            }

            const handleMouseUp = () => {
                if (!slider.value) return
                isDragging.value = false
                if (animationId.value) {
                    cancelAnimationFrame(animationId.value)
                    animationId.value = null
                }
                slider.value.style.cursor = 'grab'

                // 根据滚动距离判断当前页
                const container = slider.value
                const pageWidth = container.clientWidth
                const currentPage = Math.round(container.scrollLeft / pageWidth) + 1
                active.value = currentPage
            }

            // 响应商品数据变化
            updateTotalPages()

            return {
                slider,
                active,
                totalPages,
                items,
                itemsComputed,
                visibleCols,
                slide_position,
                slide_right,
                slide_left,
                handleMouseDown,
                handleMouseMove,
                handleMouseUp,
                handleResize
            }
        }
    }).mount('#app')
</script>
</body>
</html>